﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.Xml.Schema;

namespace BasketStats.Core.Team
{
    public class Roster : IXmlSerializable
    {
        private ICollection<PlayerInfo> m_players = new List<PlayerInfo>();

        public uint Season { get; set; }
        public string Name { get; set; }
        public ICollection<PlayerInfo> Players 
        { 
            get 
            { 
                return m_players; 
            }
            set
            {
                if (value != m_players)
                {
                    m_players = value;
                }
            }
        }
        
        public Roster()
        {
        }

        public Roster(XmlReader reader)
        {
            ReadXml(reader);
        }

        public static Roster CreateFakeRoster()
        {
            return new Roster
            {
                Players = new List<PlayerInfo>
                {
                    new PlayerInfo{
                        Name = "Davide",
                        FamilyName = "Mariani",
                        Number = 5,
                        Role = Role.GUARD,
                    },

                    new PlayerInfo{
                        Name = "Andrea",
                        FamilyName = "Galbiati",
                        Number = 13,
                        Role = Role.FORWARD,
                    },

                    new PlayerInfo{
                        Name = "Daniele",
                        FamilyName = "Mariani",
                        Number = 17,
                        Role = Role.CENTER,
                    },

                    new PlayerInfo{
                        Name = "Lorenzo",
                        FamilyName = "Cogliati",
                        Number = 6,
                        Role = Role.PLAYMAKER,
                    },

                    new PlayerInfo{
                        Name = "Luca",
                        FamilyName = "Colnaghi",
                        Number = 11,
                        Role = Role.FORWARD,
                    },

                    new PlayerInfo{
                        Name = "Roberto",
                        FamilyName = "Caglio",
                        Number = 19,
                        Role = Role.GUARD,
                    },
                },
                Season = 2012,
                Name = "Campagnola Don Bosco"
            };
        }

        public XmlSchema GetSchema()
        {
            return null;
        }

        public void ReadXml(XmlReader reader)
        {
            Season = uint.Parse(reader.GetAttribute("season"));
            Name = reader.GetAttribute("name");
            while (reader.Read() && !(reader.Name == "Roster" && reader.NodeType == XmlNodeType.EndElement))
            {
                m_players.Add(new PlayerInfo(reader));
            }
        }

        public void WriteXml(XmlWriter writer)
        {
            if(Season != 0)
            {
                writer.WriteAttributeString("season", Season.ToString());
            }
            if(Name != null)
            {
                writer.WriteAttributeString("name", Name);
            }
            foreach (var p in Players)
            {
                new XmlSerializer(p.GetType()).Serialize(writer, p);
            }
        }
    }
}
